package cn.doitedu.smart_marketing.rule;

import cn.doitedu.datagen.LogBean;
import cn.doitedu.smart_marketing.beans.ClickhouseQueryRes;
import cn.doitedu.smart_marketing.beans.CountCondition;
import cn.doitedu.smart_marketing.beans.RuleMatchResult;
import cn.doitedu.smart_marketing.querytools.ClickhouseQuerier;
import lombok.SneakyThrows;

import java.util.HashMap;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2021/7/22
 **/
public class RuleMatcher {
    ClickhouseQuerier clickhouseQuerier;
    @SneakyThrows
    public RuleMatcher(){
        clickhouseQuerier = new ClickhouseQuerier();
    }

    public RuleMatchResult matchRule(LogBean logBean){
        if(!logBean.getEventId().equals("B")) return null;

        CountCondition countCondition = new CountCondition();
        countCondition.setAccount(logBean.getAccount());
        countCondition.setEventId("B");
        countCondition.setStartTime(-1);
        countCondition.setEndTime(2811111111111L);
        HashMap<String, String> propMap = new HashMap<>();
        countCondition.setEventProps(propMap);

        ClickhouseQueryRes clickhouseQueryRes = clickhouseQuerier.queryCountCondition(countCondition);

        RuleMatchResult ruleMatchResult = null;
        if(clickhouseQueryRes.getStatus()!=500 && clickhouseQueryRes.getCount()>1) {
            ruleMatchResult = new RuleMatchResult();
            ruleMatchResult.setRuleId("test01");
            ruleMatchResult.setMatchEvent(logBean.getEventId());
            ruleMatchResult.setMatchEventTimeStamp(logBean.getTimeStamp());
            ruleMatchResult.setMatchUser(logBean.getAccount());
            ruleMatchResult.setMatchProcessTimeStamp(System.currentTimeMillis());
        }

        return ruleMatchResult;

    }

}
